package com.zudada.servermain.controller.portmanage;

import com.zudada.servermain.POJO.DTO.PortAddDTO;
import com.zudada.servermain.POJO.DTO.PortBasicUpdateDTO;
import com.zudada.servermain.POJO.DTO.PortQueryDTO;
import com.zudada.servermain.POJO.VO.PortVO;
import com.zudada.servermain.POJO.entity.PortBasic;
import com.zudada.servermain.POJO.enums.Result;
import com.zudada.servermain.Service.PortBasicService;
import com.zudada.servermain.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags="港口操作接口")
@RequestMapping("/portmanage/basic")
public class PortBasicController {
    @Autowired
    private PortBasicService portBasicService;

    /**
     * 新增港口
     */
    //对字段的限制未完善
    @PostMapping("/add")
    @ApiOperation("新增港口")
    public Result<PortBasic> addPort(@RequestBody PortAddDTO portAddDTO) {
        log.info("新增港口：{}",portAddDTO);
        portBasicService.savePort(portAddDTO);
        return Result.success();

    }

    /**
     * 删除港口
     */
    @DeleteMapping("/del")
    @ApiOperation("删除港口")
    public Result<PortBasic>delPort(String id) {
        log.info("删除港口：{}",id);
        try{
            portBasicService.delPort(id);
            return Result.success("删除港口成功");
        }catch (BusinessException e){
            return Result.error(e.getMessage());
        }catch (Exception e){
            return Result.error("系统异常，请联系管理员");
        }

    }


    /**
     * 条件查询港口列表
     */
    @GetMapping("/query")
    @ApiOperation("按条件查询港口")
    public List<PortVO> listPorts(PortQueryDTO portQueryDTO){
        log.info("条件查询港口列表",portQueryDTO);
        return portBasicService.listPorts(portQueryDTO);
    }

    /**
     * 修改港口数据
     * @param dto
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改港口数据")
    public Result<String> updatePort(@RequestBody PortBasicUpdateDTO dto) {
        portBasicService.updatePort(dto);
        return Result.success();
    }










}
